package com.lyy.seckill.controller;

import com.lyy.seckill.domain.User;
import com.lyy.seckill.redis.RedisService;
import com.lyy.seckill.redis.UserKey;
import com.lyy.seckill.result.CodeMsg;
import com.lyy.seckill.result.Result;
import com.lyy.seckill.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/demo")
public class SampleController {

    @Resource
    UserService userService;

    @Resource
    RedisService redisService;

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model) {
        model.addAttribute("name", "lyy");
        return "hello";
    }

    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello() {
        return Result.success("hello, Jesper");
    }

    @RequestMapping("/Error")
    @ResponseBody
    public Result<String> error() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet() {
        User user = userService.getById(1);
        return Result.success(user);
    }

//    @RequestMapping("/db/tx")
//    @ResponseBody
//    public Result<Boolean> dbTx() {
//        userService.tx();
//        return Result.success(true);
//    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet() {
        User user = redisService.get(UserKey.getById, "" + 1, User.class);
        return Result.success(user);
    }

//    @RequestMapping("/redis/set")
//    @ResponseBody
//    public Result<Boolean> redisSet() {
//        User user = new User();
//        user.setId(1);
//        user.setName("alianlyy");
//        Boolean flag = redisService.set(UserKey.getById, "" + 1, user);
//        return Result.success(flag);
//    }
}
